package com.piggy.logi.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.piggy.common.core.utils.PageUtils;
import com.piggy.common.core.web.page.TableDataInfo;
import com.piggy.common.satoken.utils.SecurityUtils;
import com.piggy.logi.sys.bo.LogiPickupOrderEditBo;
import com.piggy.logi.sys.bo.LogiPickupOrderQueryBo;
import com.piggy.logi.sys.domain.LogiPickupOrder;
import com.piggy.logi.sys.domain.LogiSchedule;
import com.piggy.logi.sys.domain.LogiWaybill;
import com.piggy.logi.sys.enums.WaybillStatusEnum;
import com.piggy.logi.sys.mapper.LogiPickupOrderMapper;
import com.piggy.logi.sys.service.ILogiPickupOrderService;
import com.piggy.logi.sys.service.ILogiScheduleService;
import com.piggy.logi.sys.service.ILogiWaybillService;
import com.piggy.logi.sys.vo.LogiPickupOrderExtVo;
import com.piggy.logi.sys.vo.LogiPickupOrderVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 提货单Service业务层处理
 *
 * @author piggy
 * @date 2023-08-25
 */
@Slf4j
@Service
public class LogiPickupOrderServiceImpl extends ServiceImpl<LogiPickupOrderMapper, LogiPickupOrder> implements ILogiPickupOrderService {

    @Resource
    private ILogiWaybillService waybillService;

    @Resource
    private ILogiScheduleService scheduleService;

    @Override
    public LogiPickupOrderExtVo queryById(Long id) {
        LogiPickupOrderExtVo vo = baseMapper.getById(id);
        waybillService.fillDate(Lists.newArrayList(vo.getWaybill()));
        return vo;
    }

    @Override
    public LogiPickupOrderExtVo queryByWaybillId(Long waybillId) {
        List<LogiPickupOrderExtVo> voList = baseMapper.queryList(new LogiPickupOrderQueryBo().setWaybillId(waybillId));
        if (CollUtil.isEmpty(voList)) {
            return null;
        }
        waybillService.fillDate(Lists.newArrayList(voList.get(0).getWaybill()));
        return voList.get(0);
    }

    @Override
    public TableDataInfo<LogiPickupOrderExtVo> queryPageList(LogiPickupOrderQueryBo bo) {
        PageInfo<LogiPickupOrderExtVo> result = PageHelper.startPage(bo).doSelectPageInfo(()-> {
            List<LogiPickupOrderExtVo> voList = baseMapper.queryList(bo);
            voList.stream().peek(Any-> waybillService.fillDate(Lists.newArrayList(Any.getWaybill()))).collect(Collectors.toList());
        });
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<LogiPickupOrderExtVo> queryList(LogiPickupOrderQueryBo bo, boolean ignoreWaybill) {
        List<LogiPickupOrderExtVo> voList = baseMapper.queryList(bo);
        if (!ignoreWaybill) {
            voList.stream().peek(Any-> waybillService.fillDate(Lists.newArrayList(Any.getWaybill()))).collect(Collectors.toList());
        }
        return voList;
    }

    private LambdaQueryWrapper<LogiPickupOrder> buildQueryWrapper(LogiPickupOrderQueryBo bo) {
        LambdaQueryWrapper<LogiPickupOrder> lqw = Wrappers.lambdaQuery();
        lqw.eq(ObjectUtil.isNotEmpty(bo.getWaybillId()), LogiPickupOrder::getWaybillId, bo.getWaybillId());
        lqw.like(StrUtil.isNotBlank(bo.getPickupName()), LogiPickupOrder::getPickupName, bo.getPickupName());
        lqw.eq(StrUtil.isNotBlank(bo.getContactPhone()), LogiPickupOrder::getContactPhone, bo.getContactPhone());
        lqw.eq(StrUtil.isNotBlank(bo.getPlateNumber()), LogiPickupOrder::getPlateNumber, bo.getPlateNumber());
        lqw.eq(bo.getFlowStatus() != null, LogiPickupOrder::getFlowStatus, bo.getFlowStatus());
        lqw.in(CollUtil.isNotEmpty(bo.getWaybillIds()), LogiPickupOrder::getWaybillId, bo.getWaybillIds());
        return lqw;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LogiPickupOrderExtVo insertByAddBo(LogiPickupOrderEditBo bo) {
        LogiPickupOrderVo vo = this.getVoOne(new LambdaQueryWrapper<LogiPickupOrder>().eq(LogiPickupOrder::getWaybillId, bo.getWaybillId()).last("limit 1"), LogiPickupOrderVo.class);
        if (BeanUtil.isEmpty(vo)) {
            LogiPickupOrder add = BeanUtil.toBean(bo, LogiPickupOrder.class);
            bo.setFlowStatus((long) WaybillStatusEnum.PICKUPING.getCode());
            add.setCreateBy(SecurityUtils.getPhone());
            add.setCreateTime(new Date());
            save(add);
            // 查询运单调度号
            waybillService.updateStatus(add.getWaybillId(), bo.getFlowStatus().intValue());
            LogiWaybill waybill = waybillService.getById(add.getWaybillId());
            waybillService.checkAndUpdateStatus(waybill.getScheduleNo(), WaybillStatusEnum.get(waybill.getStatus()));
            return queryById(add.getId());
        }
        return updateByEditBo(bo.setId(vo.getId()));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LogiPickupOrderExtVo updateByEditBo(LogiPickupOrderEditBo bo) {
        LogiPickupOrder update = BeanUtil.toBean(bo, LogiPickupOrder.class);
        update.setUpdateBy(SecurityUtils.getUsername());
        update.setUpdateTime(new Date());
        updateById(update);
        if (ObjectUtil.isNotEmpty(bo.getFlowStatus())) {
            waybillService.updateStatus(update.getWaybillId(), update.getFlowStatus().intValue());
            LogiWaybill waybill = waybillService.getById(update.getWaybillId());
            waybillService.checkAndUpdateStatus(waybill.getScheduleNo(), WaybillStatusEnum.get(waybill.getStatus()));
        }
        return queryById(update.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
